﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.CC.BaseClass;
using Garlic.CC.Utility;
using Garlic.BLL.Common;
using Garlic.DM.DataModel;
using Garlic.CC.WebUtility;
using Garlic.BLL;
using System.Collections.Generic;

public partial class SystemManagement_ActionEdit : BasePage
{


    #region Page load
    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {

                if (string.IsNullOrEmpty(Request["RecordID"]))
                {
                    this.MainFormView.DefaultMode = FormViewMode.Insert;
                    this.pnlActionTemplate.Visible = true;
                }
                else
                {
                    ActionId = Request["RecordID"];
                    if (Request["OperateType"] == WebUtil.OperateType_Update)
                    {
                        this.MainFormView.DefaultMode = FormViewMode.Edit;
                    }
                    else
                    {
                        this.MainFormView.DefaultMode = FormViewMode.ReadOnly;
                        this.TopToolBar.Visible = false;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    #endregion

    #region toolbar event handle
    /// <summary>
    /// 工具栏事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
        try
        {
            switch (args.Command)
            {
                case ToolbarCommand.Save:
                    SaveInfo();
                    break;
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    /// <summary>
    /// 保存页面信息
    /// </summary>
    private void SaveInfo()
    {
        try
        {

            this.Validate();
            if (!Page.IsValid)
            {
                return;
            }
            if (this.MainFormView.CurrentMode == FormViewMode.Insert)
            {
                this.MainFormView.InsertItem(true);
            }
            else if (this.MainFormView.CurrentMode == FormViewMode.Edit)
            {
                this.MainFormView.UpdateItem(true);
            }

            this.CloseVirtualWindow(true);
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    #endregion

    #region form view control event handle
    /// <summary>
    /// before inserting data
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void MainFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        try
        {

            //请修改下面的初始值:
            e.Values["Action_id"] = (e.Values["Action_id"].ToString() == "") ? SequenceManager.GetNextVal("ActionID") : e.Values["Action_id"];
            e.Values["Action_name"] = (e.Values["Action_name"].ToString() == "") ? "" : e.Values["Action_name"];
            e.Values["Action_name_cn"] = (e.Values["Action_name_cn"].ToString() == "") ? "按钮" : e.Values["Action_name_cn"];
            e.Values["Action_url"] = (e.Values["Action_url"].ToString() == "") ? "" : e.Values["Action_url"];
            if (!e.Values.Contains("Menu_id"))
            {
                DropDownList ddlMenuID = this.MainFormView.FindControl("ddlParentMenuID") as DropDownList;
                e.Values.Add("Menu_id", ddlMenuID.SelectedValue);
            }
            //e.Values["Menu_id"] = (e.Values["Menu_id"].ToString() == "") ? "初始值" : e.Values["Menu_id"];
            e.Values["Menu_order"] = (string.IsNullOrEmpty(e.Values["Menu_order"].ToString())) ? "0" : e.Values["Menu_order"];
            e.Values["Action_type"] = (e.Values["Action_type"].ToString() == "") ? 0 : e.Values["Action_type"];
            e.Values["Icon_name"] = (e.Values["Icon_name"].ToString() == "") ? "Icon-Search.png" : e.Values["Icon_name"];
            e.Values["Client_js"] = (e.Values["Client_js"].ToString() == "") ? "" : e.Values["Client_js"];
            e.Values["Section"] = (e.Values["Section"].ToString() == "") ? "Top" : e.Values["Section"];
            e.Values["Create_time"] = (e.Values["Create_time"].ToString() == "") ? DateTime.Now : e.Values["Create_time"];
            e.Values["Create_user"] = (e.Values["Create_user"].ToString() == "") ? CurrentUser.UserID : e.Values["Create_user"];
            e.Values["Modify_user"] = (e.Values["Modify_user"].ToString() == "") ? CurrentUser.UserID : e.Values["Modify_user"];
            e.Values["Modify_time"] = (e.Values["Modify_time"].ToString() == "") ? DateTime.Now : e.Values["Modify_time"];
            e.Values["Action_Description"] = (e.Values["Action_Description"].ToString() == "") ? "" : e.Values["Action_Description"];

        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    /// <summary>
    /// before updating data
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void MainFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        try
        {

            if (!e.NewValues.Contains("Menu_id"))
            {
                DropDownList ddlMenuID = this.MainFormView.FindControl("ddlParentMenuID") as DropDownList;
                e.NewValues.Add("Menu_id", ddlMenuID.SelectedValue);
            }
            e.NewValues["Modify_user"] = CurrentUser.UserID;
            e.NewValues["Modify_time"] = DateTime.Now;
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    #endregion

    #region tool method
    /// <summary>
    /// show message 
    /// </summary>
    /// <param name="msg"></param>
    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }
    #endregion


    public string ActionId
    {
        get
        {
            if (ViewState["ActionId"] == null)
            {
                return string.Empty;
            }
            return ViewState["ActionId"].ToString();
        }
        set
        {
            ViewState["ActionId"] = value;
        }
    }


    private void SetTemplate(string actionName)
    {
        ActionInfo info = ActionManager.GXYctionTemplate(actionName);
        ((TextBox)(MainFormView.FindControl("tbAction_name"))).Text = info.Action_name;
        ((TextBox)(MainFormView.FindControl("tbAction_url"))).Text = info.Action_url;
        ((TextBox)(MainFormView.FindControl("tbAction_type"))).Text = info.Action_type.ToString();
        ((TextBox)(MainFormView.FindControl("tbAction_name_cn"))).Text = info.Action_name_cn;
        ((TextBox)(MainFormView.FindControl("tbIcon_name"))).Text = info.Icon_name;
        ((TextBox)(MainFormView.FindControl("tbClient_js"))).Text = info.Client_js;
        ((TextBox)(MainFormView.FindControl("tbSection"))).Text = info.Section;
        ((TextBox)(MainFormView.FindControl("tbActionDescription"))).Text = info.Action_Description;

    }

    protected void ddlActionTemplate_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string actionTemplateName = this.ddlActionTemplate.SelectedValue;
            switch (actionTemplateName)
            {
                case ToolbarCommand.Create:
                    this.SetTemplate(ToolbarCommand.Create);
                    break;
                case ToolbarCommand.Delete:
                    this.SetTemplate(ToolbarCommand.Delete);
                    break;
                case ToolbarCommand.Export:
                    this.SetTemplate(ToolbarCommand.Export);
                    break;
                case ToolbarCommand.HideOrExpand:
                    this.SetTemplate(ToolbarCommand.HideOrExpand);
                    break;
                case ToolbarCommand.Search:
                    this.SetTemplate(ToolbarCommand.Search);
                    break;
                case ToolbarCommand.Save:
                    this.SetTemplate(ToolbarCommand.Save);
                    break;
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    protected void MainFormView_DataBound(object sender, EventArgs e)
    {
        DropDownList ddlParentMenuID = this.MainFormView.FindControl("ddlParentMenuID") as DropDownList;
        MenuManager.BindMenuList(ddlParentMenuID);
        if (this.MainFormView.DefaultMode == FormViewMode.Edit)
        {
            ActionInfo actionInfo = this.MainFormView.DataItem as ActionInfo;

            foreach (ListItem li in ddlParentMenuID.Items)
            {
                if (li.Value == actionInfo.Menu_id)
                {
                    li.Selected = true;
                    break;
                }
            }
        }
    }

    protected void ddlParentMenuID_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox actionDescription = this.MainFormView.FindControl("tbActionDescription") as TextBox;
        TextBox actionName = this.MainFormView.FindControl("tbAction_name_cn") as TextBox;
        DropDownList ddlParentMenu = sender as DropDownList;
        if (actionDescription != null && ddlParentMenu != null && actionName != null)
        {
            actionDescription.Text = ddlParentMenu.Text + " -- " + actionName.Text;
        }
    }

    protected void valiDuplicated_ServerValidate(object source, ServerValidateEventArgs args)
    {

        string actionName = ((TextBox)this.MainFormView.FindControl("tbAction_name")).Text;
        string actionUrl = ((TextBox)this.MainFormView.FindControl("tbAction_url")).Text;
        List<ActionInfo> actionList = ActionManager.GetList(string.Format("Action_name='{0}' AND Action_url='{1}'", actionName, actionUrl), string.Empty);
        if (actionList != null && actionList.Count > 0)
        {
            if (string.IsNullOrEmpty(ActionId))//insert
            {
                args.IsValid = false;
            }
            else //update
            {
                args.IsValid = true;
                foreach (ActionInfo actionInfo in actionList)
                {
                    if (string.Compare(actionInfo.Action_id.ToString(), ActionId) != 0)
                    {
                        args.IsValid = false;
                        break;
                    }
                }
            }
        }
        else
        {
            args.IsValid = true;
        }
    }

}


